/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.gui.GUIModule;
import tal.drivechain.gui.TextButton;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.SetProperty.Flag;
import tal.drivechain.variable.VarTableAction;
import tal.drivechain.variable.Variable;


    
/**
 *
 * @author Main
 */
public class Button extends GUIModule implements ActionListener {
    Variable onClick;
    TextButton button;
    VarTableAction setState;
    Variable state;

    
    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        component = button = new TextButton((String)props.getProperty("label"), color, font);
        super.setupProperties();

        state = new Variable("state", DataType.BOOLEAN, false, true);
        setState = new VarTableAction("state", VarTableAction.Action.SET_ANY);
        button.addActionListener(this);
        button.setNegative(negative);
        button.setEnabled(enabled);
    }

    public void setToggleButton(Boolean toggle) { button.setToggleButton(toggle); }
    public boolean getToggleButton() { return button.isToggleButton(); }

    @Override
    public void setNegative(Boolean value) {
        super.setNegative(value);
        if (button==null) return;
        button.setNegative(negative);
        button.repaint();
    }

    @Override
    public void setFont(Font value) {
        super.setFont(value);
        if (button!=null) {
            button.setFont(font);
            relayout();
        }
    }

    @Override
    public void setColor(Color value) {
        super.setColor(value);
        if (button!=null) {
            button.setColor(color);
        }
    }

    @Override
    public void setEnabled(Boolean value) {
        super.setEnabled(value);
        if (button!=null) button.setEnabled(enabled);
    }

    public void setHasBorder(Boolean value) {
        button.setHasBorder(value);
        button.repaint();
    } public boolean getHasBorder() { return button.hasBorder(); }

    public void setOnClick(Variable value) {
        onClick = value;
    } public Variable getOnClick() { return onClick; }

    public void setLabel(String value) {
        button.setText(value);
        relayout();
    } public String getLabel() { return button.getText(); }

    public void actionPerformed(ActionEvent e) {
        state.setValue(button.isSelected(), true);
        setState.set(state, localVars, this);
        sendToOutputs(onClick, -1);
    }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
    }
}    

